home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / Toolbox / ReKeyTransƒ / ReKeyTrans.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  4.3 KB  |  170 lines  |  [TEXT/MPS ]

  1. /*******************************************************************************
  2.  
  3.     OpenWindow by Cameron
  4.     
  5. *******************************************************************************/
  6.  
  7. #include <String.h>
  8.  
  9. /* Type 1 includes */
  10. #include <Types.h>
  11. #include <QuickDraw.h>
  12.  
  13. /* Type 2 includes */
  14. #include <Controls.h>
  15. #include <Events.h>
  16. #include <Fonts.h>
  17. #include <Memory.h>
  18. #include <Menus.h>
  19. #include <OSUtils.h>
  20. #include <Resources.h>
  21. #include <SegLoad.h>
  22. #include <TextEdit.h>
  23. #include <ToolUtils.h>
  24. #include <Traps.h>
  25. #include <Script.h>
  26.  
  27. /* Type 3 includes */
  28. #include <Desk.h>
  29. #include <Files.h>
  30. #include <OSEvents.h>
  31. #include <Windows.h>
  32.  
  33. /* Type 4 includes */
  34. #include <Dialogs.h>
  35.  
  36. /*  Global Variables  */
  37.  
  38. Str255            WindTitle;
  39. WindowPtr        MyWindow,aWindPtr;
  40. short            err,keycode,StartV,StartH;
  41. EventRecord        MyEvent;
  42. Boolean            quit,DrawOn;
  43. Rect            WindRect,Rect1,Rect2;
  44. Point            aPoint,lastPoint;
  45. long            KCHRID,newKey,state;
  46. Handle            KCHRHdl;
  47.  
  48. extern    void    PATCHKEYTRANS();
  49. extern    void    UNPATCH();
  50.  
  51. /*************************************************************************************/
  52. void    InitMac()
  53. {
  54.     InitGraf(&qd.thePort);
  55.     InitFonts();
  56.     FlushEvents(everyEvent,0);
  57.     InitWindows();
  58.     InitCursor();
  59.     quit = false;
  60.     DrawOn = false;
  61.     StartV = 25;
  62.     StartH = 5;
  63. }
  64. /*************************************************************************************/
  65. void    DoCR()
  66. {
  67.     if (lastPoint.v > (*MyWindow).portRect.bottom-15)
  68.     {
  69.         EraseRect(&(*MyWindow).portRect);
  70.         lastPoint.v = StartV;
  71.         lastPoint.h = StartH;
  72.     }
  73.     else
  74.     {
  75.         lastPoint.v = lastPoint.v+14;
  76.         lastPoint.h = StartH;
  77.     }
  78. }
  79. /*************************************************************************************/
  80. void    DoDraw()
  81. {
  82.     DrawOn = true;
  83.     GlobalToLocal(&MyEvent.where);
  84.     MoveTo(MyEvent.where.h,MyEvent.where.v);
  85.     EraseRect(&WindRect);
  86. }
  87. /*************************************************************************************/
  88. void    DoKeyDown()
  89. {
  90.     if (BitAnd(MyEvent.message,charCodeMask) == 'q'        // if the user typed the magic "cmd-q" sequence,
  91.     && BitAnd(MyEvent.modifiers,0x0100))
  92.         { quit = true; }                                // it's Miller time!
  93.     else    {
  94.         MoveTo(lastPoint.h,lastPoint.v);
  95.         newKey = BitAnd(MyEvent.message,keyCodeMask);    // Strip out all but the keycode
  96.         newKey = BitShift(newKey,-8);                    // move the keycode down to the low byte
  97.         keycode = LoWord(newKey);                        // extract the low word of the event message (ignore modifier bits)
  98.         keycode = keycode & 0xFF7F;                        // make sure it looks like a key down stroke
  99.         newKey = KeyTrans(*KCHRHdl, keycode, &state);    // have KeyTrans translate the key code to ASCII
  100.         keycode = LoWord(newKey);                        // now get the ASCII 2 value (IM5, p195 has it backward!)
  101.         DrawChar(keycode);                                // just to make sure, let's have a look
  102.         GetPen(&lastPoint);                                // now make sure we stay inside the content region
  103.         if (lastPoint.h > ((*MyWindow).portRect.right-10))    // if we're getting to close to the edge,
  104.             {     DoCR();    }                                // go fix it up
  105.     }
  106. }
  107. /*************************************************************************************/
  108. main()
  109. {
  110.     InitMac();
  111.     
  112.     MyWindow = GetNewWindow(2009,nil,(WindowPtr)-1);
  113.  
  114.     if (MyWindow)
  115.     {
  116.         SetPort(MyWindow);
  117.         MoveTo(StartH,StartV);
  118.         lastPoint.h = StartH;
  119.         lastPoint.v = StartV;
  120.         KCHRID = GetScript(smRoman, smScriptKeys);    /* returns resource ID of KCHR being used by system */
  121.         KCHRHdl = GetResource('KCHR',KCHRID);
  122.         if (KCHRHdl)
  123.         {
  124.             MoveHHi(KCHRHdl);
  125.             HLock(KCHRHdl);
  126.             PATCHKEYTRANS();
  127.             while (quit != true)
  128.             {
  129.                 if (DrawOn)
  130.                 {
  131.                     if (StillDown())
  132.                     {
  133.                         GetMouse(&aPoint);
  134.                         StdLine(aPoint);
  135.                     }
  136.                     else { DrawOn = false; }
  137.                 }
  138.                 else
  139.                 {
  140.                     if (WaitNextEvent(everyEvent,&MyEvent,0,nil))
  141.                     {
  142.                         switch (MyEvent.what)
  143.                         {
  144.                             case mouseDown:
  145.                             {
  146.                                 if ((FindWindow(MyEvent.where,&aWindPtr) == inContent)        // make sure the mouseDown is 
  147.                                 && (aWindPtr == MyWindow))                                    // where we want it
  148.                                     { DoDraw(); }                                            // set up for drawing a line
  149.                                 else if (FindWindow(MyEvent.where,&aWindPtr) == inGoAway)    // on the other hand, if it's in the goAway box,
  150.                                     { quit = true; }                                        // we can take a hint!
  151.                                 break;
  152.                             }
  153.                             case keyDown:
  154.                             {
  155.                                 DoKeyDown();
  156.                                 break;
  157.                             }
  158.                             case autoKey:
  159.                             {
  160.                                 DoKeyDown();
  161.                                 break;
  162.                             }
  163.                         }
  164.                     }
  165.                 }
  166.             }
  167.             UNPATCH();
  168.         }
  169.     }
  170. }